import axios from '@/axios/http'
import _ from 'lodash'
import routers from '@/router'
import { querystring,Alert  } from 'vux'
import QS from 'qs'
let listView =   {
    namespaced:true,
    state: {
        editing:false,
        validaty:0,//0:代表选择人的模板，1：代表选择物的模板
        addMore:false,
        showToast:false,
        dialogIndex:'',
        personData:{
          value:'标签',
          key:0,
          children:[
          {
            value:'默认名片',
            key:10000,
            lists:[
              {key:'1-1',value:'例子'},
            ],
          }
          ]
        },
        goodsData:{
          value:'标签',
          key:0,
          children:[
            {
              value:'默认名片',
              key:10000,
              lists:[
                {key:'1-1',value:'张三'},
                {key:'1-2',value:'李四'},
                {key:'1-3',value:'王五'},
              ],
            },
            {
              value:'人物名片',
              key:1,
              lists:[
                {key:'1-1',value:'名片1'},
                {key:'1-2',value:'名片2'},
                {key:'1-3',value:'名片3'},
              ]
            },
            {
              value:'实物名片',
              key:2,
              lists:[
                {value:'名片4',key:'2-1'},
                {value:'名片5',key:'2-2'},
                {value:'名片6',key:'2-3'},
              ]
            },
            {
              value:'其他名片',
              key:3,
              lists:[
                {value:'名片7',key:'3-1'},
                {value:'名片8',key:'3-2'},
                {value:'名片9',key:'3-3'},
                {value:'名片10',key:'3-4'},
                {value:'名片11',key:'3-5'},
                {value:'名片12',key:'3-6'},
                {value:'名片13',key:'3-7'},
                {value:'名片14',key:'3-8'},
                {value:'名片15',key:'3-9'},
              ]
            }
          ]
        },

      },
      getters:{
        personGetters:(state,vals)=>{
          //console.log(state.personData.children[0].lists)
          return state.personData.children[0].lists
        },
        goodsGetters:(state,val)=>{
          return state.goodsData.children[0].lists
        },
      },
      mutations: {
        addtitles(state,payload){
          if(state.validaty==0){
            state.personData.children.push({
                key: Math.floor(Math.random()*1000),
                value: payload ? payload : '未命名标签',
                lists:[]
            })
          }else{
            state.goodsData.children.push({
                key: Math.floor(Math.random()*1000),
                value: payload ? payload : '未命名标签',
                lists:[]
            })
          }
        },
        updateIndex(state,payload){
          state.dialogIndex = payload
        },
        updateDialog(state,payload){
          payload.forEach(function(items,index,array){
            if(state.validaty==0){
              console.log(items)
              let j = _.findIndex(state.personData.children[0].lists, function(o) { return o.key == items; });
              if(state.personData.children[0].lists[j].value){
                let value = state.personData.children[0].lists[j].value
                state.personData.children[0].lists.splice(j,1)
                let objs = {}
                let dI = _.findIndex(state.personData.children, function(o) { return o.key == state.dialogIndex; });
                let lent = state.personData.children[dI].lists.length+1
                objs.key = state.dialogIndex+'-'+lent
                objs.value=value
                state.personData.children[dI].lists.push(objs)
              }else{
                return
              }
            }else{
              let j = _.findIndex(state.goodsData.children[0].lists, function(o) { return o.key == items; });
              let value = state.goodsData.children[0].lists[j].value
              state.goodsData.children[0].lists.splice(j,1)
              let objs = {}
              let lent = state.goodsData.children[state.dialogIndex].lists.length+1
              objs.key = state.dialogIndex+'-'+lent
              objs.value=value
              state.goodsData.children[state.dialogIndex].lists.push(objs)
            }
            state.showToast = false
          })
        },
        removeChild(state,payload){
          if(state.validaty==0){
            let I = _.findIndex(state.personData.children, function(o) { return o.key == payload; });
            let newarr = state.personData.children[I].lists
            var others = _.concat(state.personData.children[0].lists,newarr)
            state.personData.children[0].lists = others
            //delete state.personData.children[I]
            //_.dropWhile(state.personData.children, function(o) { return o.key==I; });
            state.personData.children.splice(I, 1)
          }else{
            let I = _.findIndex(state.goodsData.children, function(o) { return o.key == payload; });
            let newarr = state.goodsData.children[I].lists
            var others = _.concat(state.goodsData.children[0].lists,newarr)
            state.goodsData.children[0].lists = others
            state.goodsData.children.splice(I, 1)
          }
        },
        changeDialog(state,payload){
          state.showToast = payload
        },
        validaty(state,payload){
        state.validaty = payload
        },
        addstatus(state,payload){
        state.addMore = payload
        },
        changeEdit(state,payload){
         state.editing = payload
        },
        startList(state,payload){
          if(payload.person!=''){
          state.personData.children = payload.person
          }
          if(payload.events=''){
          state.goodsData.children = payload.events
          }
        }
      },
      actions:{
       getListContent({commit,state},values){
        let datas = {openid:values}
        axios.post('/public/mxcard/Wxcard/mxDeassign',querystring.stringify(datas))
          .then(function (response) {
            //console.log(response.data)
            if(response.data.status==404){
              routers.replace('/login')
            }else{
              commit('startList',response.data)
            }
          })
          .catch(function (error) {
            console.log(error);
          });
      }
      },
}
export default listView;